package com.taotao.cart.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.cart.jedis.JedisClient;
import com.taotao.cart.service.CartService;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.util.JsonUtils;
import com.taotao.pojo.TbItem;
/**
 * 服务层
 * @title CartServiceImpl
 * <p>description</p>
 * <p>company: www.itheima.com</p>
 * @author 天火
 * @version 1.0
 */
@Service
public class CartServiceImpl implements CartService{
	
	@Autowired
	private JedisClient jedisclient;
	
	@Value("${TT_CART_REDIS_PRE_KEY}")
	private String TT_CART_REDIS_PRE_KEY;

	@Override
	public TaotaoResult addItemCart(Long userId, TbItem tbItem, Integer num) {
		//1.从redis数据库中查询该用户是否已经拥有该商品
		//2.判断，如果该商品在购物车中
		TbItem itemR = queryItemByUserIdAndItemId(userId,tbItem.getId());
		if(itemR == null) {
			//3.如果商品不存在则，将该商品加到购物车中
			tbItem.setNum(num);
			//图片内容设置为一张
			String images = tbItem.getImage();
			if(StringUtils.isNoneBlank(images)) {
				tbItem.setImage(images.split(",")[0]);
			}
			//设置购买数量
			//将对象转换成POJO的JSON存储到redis中
			jedisclient.hset(TT_CART_REDIS_PRE_KEY+":"+userId, tbItem.getId()+"", JsonUtils.objectToJson(tbItem));

		}else {
		//4.如果商品存在则，则获取相关商品的数据，数量加一
			itemR.setNum(itemR.getNum() +num);
			//将购物车重新设置回去
			jedisclient.hset(TT_CART_REDIS_PRE_KEY+":"+userId, itemR.getId()+"", JsonUtils.objectToJson(itemR));
		}
		
		return TaotaoResult.ok();
	}

	public TbItem queryItemByUserIdAndItemId(Long userId, Long itemId) {
		// TODO Auto-generated method stub
		String jsonStr = jedisclient.hget(TT_CART_REDIS_PRE_KEY+":"+userId, itemId +"");
		if(StringUtils.isNotBlank(jsonStr)) {
			TbItem item = JsonUtils.jsonToPojo(jsonStr, TbItem.class);
			return item;
		}else {
			return null;
		}
		
	}

	@Override
	public List<TbItem> getCartList(Long userId) {
		Map<String, String> map = jedisclient.hgetAll(TT_CART_REDIS_PRE_KEY+":"+userId + "");
		//map转为
		List<TbItem> list = new ArrayList<>();
		if (map != null) {
			for (Map.Entry<String, String> entry : map.entrySet()) {
				String value = entry.getValue();// 商品的jSON数据
				// 转成POJO
				TbItem item = JsonUtils.jsonToPojo(value, TbItem.class);
				list.add(item);
			}
		}
		return list;
	}

	@Override
	public TaotaoResult updateItemCartByItemId(Long userId, Long itemId, Integer num) {
		//1.根据用户id和商品的id获取商品的对象
		TbItem tbItem = queryItemByUserIdAndItemId(itemId,userId);
		//判断是否存在
		if(tbItem!=null){
			//2.更新数量
			tbItem.setNum(num);
			//设置回redis中
			jedisclient.hset(TT_CART_REDIS_PRE_KEY+":"+userId, itemId+"", JsonUtils.objectToJson(tbItem));
		}else{
			//不管啦
		}
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteItemCartByItemId(Long userId, Long itemId) {
		jedisclient.hdel(TT_CART_REDIS_PRE_KEY+":"+userId, itemId+"");
		return TaotaoResult.ok();
	}


}
